package com.ruoyi.tmsdriveracc.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.tmsdriveracc.domain.DriverCheck;
import com.ruoyi.tmsdriveracc.domain.Receipt;
import com.ruoyi.tmsdriveracc.domain.Waybill;
import com.ruoyi.tmsdriveracc.mapper.DriverCheckMapper;
import com.ruoyi.tmsdriveracc.service.DriverCheckService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: DriverCheckServiceImpl
 * @Description: TODO
 * @Author 小何
 * @Date 2022/4/20 17:08
 * @version: 1.0
 */
@Service
public class DriverCheckServiceImpl implements DriverCheckService {
    @Autowired(required = false)
    private DriverCheckMapper driverCheckMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public List<DriverCheck> selectDcList(DriverCheck driverCheck) {

        return driverCheckMapper.selectDcList(driverCheck);
    }


    //    司机验收
    @Override
    @Transactional
    public int updateDriverCheck(Receipt receipt) {

        //消息队列通知
        //通过合同拿到发货人和收货人手机号
        Long waybillId = receipt.getWaybillId();
        Waybill waybill = new Waybill();
        waybill=driverCheckMapper.selectPhoneByContractId(waybillId);

        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("Mobile", waybill.getSenderMobile());
        HashMap<Object, Object> map2 = new HashMap<>();
        map2.put("Mobile", waybill.getReceiverMobile());
//        消息通知
//        发给发货人
        rabbitTemplate.convertAndSend("queueSms", JSON.toJSONString(map1));
//        发给收货人
        rabbitTemplate.convertAndSend("queueSms", JSON.toJSONString(map2));

        //增加回执
        driverCheckMapper.insertReceipt(receipt);
        //更新状态

        int i = driverCheckMapper.updateDriverCheck(receipt);
        System.out.println(i+"========");
        return i;
    }
}
